import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    discussion: {
      user: "小明",
      question: "问题",
      content: "如何在大学找兼职？",
      file: "课件1",
      position: 11
    },
    teacher: false,
    teacherName: "张老师",
    chats: [
      {
        user: "小明",
        content: "1+1为什么等于2"
      },
      {
        user: "小红",
        content: "1+2=3"
      }
    ],
    discussions: [],
    students: [
      {
        name: "同学A",
        progress: 9,
        levels: [
          -0.3,
          0.3,
          0.5,
          0.8,
          0.4,
          0.2,
          0.3,
          0.7,
          0.2,
          0.1,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ]
      },
      {
        name: "同学B",
        progress: 8,
        levels: [
          0.1,
          0.1,
          0.5,
          0.2,
          0.4,
          0.2,
          0.3,
          0.2,
          0.3,
          0.1,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ]
      },
      {
        name: "同学C",
        progress: 10,
        levels: [
          0.1,
          0.2,
          0.3,
          0.2,
          0.4,
          0.2,
          0.4,
          0.1,
          0.3,
          0.1,
          0.2,
          0.2,
          0.3,
          0.2,
          0,
          0,
          0,
          0,
          0,
          0
        ]
      }
    ]
  },
  mutations: {
    SET_QUESTION(state, token) {
      state.discussion.question = token;
    },
    SET_CONTENT(state, token) {
      state.discussion.content = token;
    },
    SET_TEACHER(state, token) {
      state.teacher = token;
      console.log("teacher: ", token);
    },
    ADD_CHAT(state, token) {
      state.chats.push(token);
    },
    ADD_DISCUSSION(state, token) {
      state.discussions.push(token);
    },
    CLEAR_DISCUSSION(state) {
      state.discussions = [];
    }
  },
  getters: {
    levels: state => {
      if (state.students.length > 0) {
        const len = state.students[0].levels.length,
          n = state.students.length,
          levels = [];
        let sum;
        for (let i = 0; i < len; i++) {
          sum = 0;
          for (let j = 0; j < n; j++) {
            sum += state.students[j].levels[i];
          }
          levels.push(sum / n);
        }
        return levels;
      } else {
        return [];
      }
    }
  },
  actions: {},
  modules: {}
});
